package com.tao.china.translate.impl.baidu;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.tao.china.common.http.HttpClientUtil;
import com.tao.china.common.http.porxy.PorxyEntry;
import com.tao.china.translate.Language;
import com.tao.china.translate.TranslateExecution;


/**
 * 百度翻译实现
 * @author liuchao
 * @version $Id: BaiduTranslate.java,v 0.1 2012-10-9 下午11:05:15 liuchao Exp $
 */
public class BaiduTranslate implements TranslateExecution {
    
    private  final static String url = "http://openapi.baidu.com/public/2.0/bmt/translate";


    @Override
    public String translate(Language fromLanguage, Language toLanguage, String content,
                            PorxyEntry porxy) throws Exception {
        Map<String, String> parame = new HashMap<String,String>();
        parame.put("client_id", "3QWsqZ0C5XEKVBgRQ56Kigjb");
        parame.put("q", content);
        parame.put("from", fromLanguage.toString());
        parame.put("to", fromLanguage.toString());
        byte[] jsonByte = HttpClientUtil.postSimpleMethodHttpClent(url, parame,porxy);
        if( null != jsonByte ){
            String json = new String( jsonByte,"UTF-8");
            if( null != json ){
                BaiduTransResult baiduTransResult = JSON.parseObject(json.trim(),BaiduTransResult.class);
                List<TransResult> transResult = baiduTransResult.getTrans_result();
                if( null!= transResult && transResult.size() > 0 ){
                    return transResult.get(0).getDst();
                }
            }
        }
        return null;
    }

}
